package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.PageBean;
import com.zzyl.nursing.domain.Qianduan;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<AlertData> getAllAlertData(AlertData alertData, LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<AlertData> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(alertData.getStatus() != null, AlertData::getStatus, alertData.getStatus());
        queryWrapper.eq(alertData.getDeviceName() != null, AlertData::getDeviceName, alertData.getDeviceName());
        if (startTime != null) {
            Date startData = DateUtils.toDate(startTime);
            Date endDate = DateUtils.toDate(endTime);
            queryWrapper.between(AlertData::getCreateTime, startData, endDate);
        }
        return list(queryWrapper);
    }

    @Override
    public void handleAlertData(Qianduan alertData) {

        //我的alertData.getProcessingTime()类型为2024-12-16T16:00Z，转换为yyyy-MM-dd HH:mm:ss格式
        String input = alertData.getProcessingTime().toString();

        // 解析输入时间戳为 ZonedDateTime
        ZonedDateTime utcTime = ZonedDateTime.parse(input);

        // 调整到中国标准时间（CST）
        ZonedDateTime cstTime = utcTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));

        // 将 CST ZonedDateTime 转换为 LocalDateTime
        LocalDateTime localDateTime = cstTime.toLocalDateTime();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        AlertData alertData1 = new AlertData();
        alertData1.setId(alertData.getId());
        alertData1.setProcessingResult(alertData.getProcessingResult());
        alertData1.setProcessingTime(localDateTime);
        alertData1.setStatus(1);
        alertData1.setProcessorId(userId);
        alertData1.setProcessorName(username);
        alertDataMapper.updateAlertData(alertData1);

    }


}
